import React, { useState, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import axios from 'axios'
import baseurl from '../../axios/config'
import { message } from 'antd'
export default function Home() {
  // 声明路由跳转钩子
  const navigator = useNavigate()
  // 验证登录
  const token = localStorage.getItem('token')
  if (!token) {
    navigator('/err')
  }

  const xiaolu = localStorage.getItem('xiaolu')
  const [leg, setleg] = useState('')

  const [gd, setgd] = useState('')
// 进行数据处理

  useEffect(() => {
    const fatch = async () => {
      await axios
        .get('http://node.dayuan0307.top:300/gf')
        .then(async (resa) => {
          setgd(resa.data)
          await axios
            .get('http://127.0.0.1:31401')
            .then(async (res) => {
              const a = res.data.core_latest_ledger
              const b = resa.data

              const a1 = a === 1
              const c = a - b
            
              const c1 = c < 1200 || c < 0
              const c2 = c > 5000
              if (a1) {
                setleg('新作')
                await axios
                  .post('http://node.dayuan0307.top:300/ed', {
                    name: xiaolu,
                    status: '新作'
                  })
                  .then((res) => {
                    message.info('新作提交成功')
                  })
              }
              if (c1) {
                setleg('正常')
                 axios
                  .post('http://node.dayuan0307.top:300/ed', {
                    name: xiaolu,
                    status:'正常'
                  })
                  .then((res) => {
                    message.info('正常提交成功')
                  })
              }
              if (c2) {
                setleg('延迟')
                await axios
                  .post('http://node.dayuan0307.top:300/ed', {
                    name: xiaolu,
                    status: '延迟'
                  })
                  .then((res) => {
                    message.info('延迟提交成功')
                  })
              }
            })
            .catch(async (err) => {
              setleg('不正常')
              await axios
                .post('http://node.dayuan0307.top:300/ed', {
                  name: xiaolu,
                  status: '不正常'
                })
                .then((res) => {
                  message.info('不正常提交成功')
                })
            })
        })
        .catch((err) => {
          setleg('官方区块获取失败')
        })
    }

    fatch()

    const da = setInterval(() => {
      fatch()
      // 15分钟提价数据
    }, 900000)

    return () => {
      clearInterval(da)
    }
  }, [])
  console.log(leg)
  return (
    <div>
      <h1>
        本地pi节点运行情况<h1 style={{ color: 'red' }}>{leg}</h1>
      </h1>
      <br></br>
      <h1>当前登录用户:{xiaolu}</h1>
      {/* <br></br>
      
<h1>电话：19398792867</h1><br></br>
 <h1>微信昵称:"节点阿豹"</h1><br></br>
 <h1> 微信号:751312329</h1>
  <br></br>
<h1>本工具版本归属于：节点-阿豹工作室定制开发</h1> */}
    </div>
  )
}
